import torch as t
import torch.nn.functional as F
import dataloader
import numpy as np
import pandas as pd
import os
import get_rid_of as grf

# def lc_m():
os.environ['CUDA_VISIBLE_DEVICES'] = '0'              #指定gpu
model = t.load("lc_model/save/40000 model.pkl")                #加载模型与参数
model.eval()                                             #模型进入测试状态

namelist = []
predicted = []
test_pred = t.FloatTensor()
for i, (data, name) in enumerate(dataloader.testloader):
    # print("这是第",i,"次")
    # if t.cuda.is_available():
    #     data = data.cuda()  # 放入gpu

    outputs = model(data)
    test_pred = t.cat((test_pred, outputs.data), dim=0)
    name = list(name)
    namelist.extend(name)

TEST=pd.DataFrame(test_pred.numpy(),index=namelist,columns=['GBDT_FTRL'])           #结果中‘GBDT_FTRL’列表示输出的概率，经过sigmod激活
TEST['result']=0

thread = TEST['GBDT_FTRL'].quantile(0.72)
# print(thread)

index1=TEST[TEST['GBDT_FTRL']>thread].index
# print(len(index1))                                                                    #输出“1”的个数
TEST.loc[index1,'result']=1                                                          #结果中‘result’列表示输出的结果，经过sigmod激活
# result=pd.DataFrame(y_predict[0],index=xgb_onehotDfforTest.index,columns=['GBDT_FTRL'])
TEST.to_csv('GBDT_FTRL.csv')                                                        #输出结果


TEST=TEST.drop('GBDT_FTRL',axis=1)                                                  #丢掉概率值那一列

TEST.to_csv('GBDT_FTRL.csv')

grf.inhance()                                                                        #运行去除离群点
os.remove('GBDT_FTRL.csv')
print("lc_model sucessed!")
